<script>
import { mapState } from 'vuex'
import { config, formConfig, column } from './formConfig.js'
import SearchManage from '@/components/basic/SearchManage.vue'
import { requestApi } from '@/requset/api'

export default {
    extends: SearchManage,
    data() {
        return {
            table_title: '用户',
            url: 'user',
            dialog: {
                labelWidth: '100px',
                config: [],
                form: {},
                title: '',
            },
            actions: [
                {
                    label: '添加',
                    icon: 'el-icon-folder-add',
                    click: () => {
                        this.handleAdd()
                    },
                    if: () => this.operate,
                },
            ],
            queryForm: {
                page: 1,
                size: 20,
                scope_id: null,
                nick_name: '',
                scope_id_list: [],
            },
            queryFormC: {
                page: 1,
                size: 20,
                nick_name: null,
                scope_id: null,
                scope_id_list: [],
            },
            formConfig: formConfig,
            baseForm: { user_name: '', password: '', nick_name: '', role_id: null, phonenumber: null, notify: 4, speech: 4, mail: 4, message: 4 },
        }
    },
    computed: { ...mapState('backstage', ['roleList', 'backstageData']) },
    methods: {
        BeforeAdd() {
            if (this.dialog.config.length === 8) {
                this.dialog.config.splice(3, 0, {
                    name: 'password',
                    type: 'input',
                    placeholder: '请输入密码',
                    required: true,
                    label: '密码',
                })
            }
        },
        BeforeEdit() {
            if (this.dialog.config.length === 9) {
                this.dialog.config.splice(3, 1)
            }
        },
        handleReset(val) {
            requestApi('post', this.url + '/reset', val).then((res) => {
                if (res.data.code == 0) {
                    this.$message({
                        type: 'success',
                        message: '用户密码已重置为123456',
                    })
                    this.getlist()
                }
            })
        },
    },
    created() {
        this.formConfig[1].options = [
            {
                label: '超级管理员',
                value: 0,
                children: this.backstageData,
            },
        ]
        column[column.length - 1].buttons = [
            {
                text: '编辑',
                click: (val) => {
                    this.handleEdit(val)
                },
                icon: 'el-icon-edit',
            },
            {
                text: '重置密码',
                click: (val) => {
                    this.handleReset(val)
                },
                icon: 'el-icon-refresh',
            },
            {
                text: '删除',
                click: (val) => {
                    this.delete(val)
                },
                icon: 'el-icon-delete',
            },
        ]
        column[column.length - 1].if = this.operate
        this.dialog.config = config
        this.tableColumn = column
        this.getlist()
        this.dialog.config[0].options = this.roleList
        this.tableColumn[4].format = (val) => {
            const role = this.roleList.find((item) => item.id === val)
            return role ? role.desc : ''
        }
    },
}
</script>
